package com.linewow.xhyy.nihaonew.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LXR on 2016/9/19.
 */
public class ListBaseFragment extends Fragment {
    private String TAG = "ListBaseFragment";
    @Bind(R.id.list_base_no_data_tv)
    TextView listBaseNoDataTv;
    @Bind(R.id.list_base_no_data)
    FrameLayout listBaseNoData;
    @Bind(R.id.list_base_no_internet_tv)
    TextView listBaseNoInternetTv;
    @Bind(R.id.list_base_no_internet_btn)
    Button listBaseNoInternetBtn;
    @Bind(R.id.list_base_no_internet)
    RelativeLayout listBaseNoInternet;
    @Bind(R.id.list_base_include)
    protected RelativeLayout listBaseInclude;
    @Bind(R.id.list_base_content)
    protected FrameLayout listBaseContent;
    @Bind(R.id.list_base_loading)
    FrameLayout listBaseLoading;
    @Bind(R.id.list_base_content_no_login)
    protected RelativeLayout noLoginRela;
    @Bind(R.id.no_login_bg_icon)
    ImageView noLoginBgIcon;
    @Bind(R.id.no_login_message_content)
    TextView noLoginMessageContent;
    @Bind(R.id.no_login_btn)
    RelativeLayout noLoginBtn;
    protected ListBaseListener listener;

    private String analysisName;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        analysisName=this.getClass().getCanonicalName();
        View view = View.inflate(getActivity(), R.layout.fragment_list_base, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        goneView();
        setUpView();
        initList();
    }

    protected void initList() {

    }

    protected void setUpView() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void onNoDataView() {
        listBaseNoData.setVisibility(View.VISIBLE);
    }

    public void onNoInternetView() {
        goneView();
        listBaseNoInternet.setVisibility(View.VISIBLE);
    }

    public void onLoadingView() {
        goneAllView();
        listBaseLoading.setVisibility(View.VISIBLE);
        listBaseContent.setVisibility(View.VISIBLE);
    }

    public void finishLoadingView(){
        listBaseLoading.setVisibility(View.GONE);
        listBaseNoData.setVisibility(View.GONE);

    }


    /**
     * 如果是false的话 就和登录无关
     * @return
     */
    protected  boolean aboutFlag(){
        return  true;
    }

    public void goneView() {
        listBaseNoInternet.setVisibility(View.GONE);
        listBaseLoading.setVisibility(View.GONE);

    }

    private void goneAllView(){
        listBaseNoInternet.setVisibility(View.GONE);
        listBaseLoading.setVisibility(View.GONE);
        listBaseContent.setVisibility(View.GONE);
        noLoginRela.setVisibility(View.GONE);

    }

    private void onLoginView() {
        goneView();
        noLoginRela.setVisibility(View.GONE);
        listBaseContent.setVisibility(View.VISIBLE);
    }

    private void onNoLoginView() {
        goneView();
        noLoginRela.setVisibility(View.VISIBLE);
        listBaseContent.setVisibility(View.GONE);
    }

    public void setLogin(boolean loginFlag) {
        if (loginFlag) {
            onLoginView();
        } else {
            onNoLoginView();
        }
    }

    public void showError(String string) {
        goneView();
        listBaseContent.setVisibility(View.GONE);//需要把主要的view去掉
        listBaseNoInternet.setVisibility(View.VISIBLE);
        listBaseNoInternetTv.setText(string);

    }

    @Override
    public void onResume() {
        super.onResume();
        if(aboutFlag()){
            Log.e(TAG,"是否刷新身份");
            setLogin(NiHaoApp.flag);//每次 刷新 显示不同的画面
        }else{
            noLoginRela.setVisibility(View.GONE);
            listBaseContent.setVisibility(View.VISIBLE);
        }
        MobclickAgent.onPageStart(analysisName);
    }

    @OnClick({R.id.list_base_no_internet_btn, R.id.no_login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.list_base_no_internet_btn:
                if (listener != null) {
                    listener.onRetryListener();
                }
                break;
            case R.id.no_login_btn:
                if (listener != null) {
                    listener.onLoginListener();
                }
                break;
        }
    }


    public interface ListBaseListener {
        void onLoginListener();
        void onRetryListener();
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(analysisName);
    }
}
